package cn.bdqn.pn.service.user.impl;

import cn.bdqn.pn.pojo.order.MainOrder;
import cn.bdqn.pn.pojo.user.Admin;
import cn.bdqn.pn.reposity.user.AdminReposity;
import cn.bdqn.pn.service.user.AdminService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminReposity adminReposity;

    @Override
    public Admin backstageLogin(String adminAccount, String adminPwd) {
        return adminReposity.findAdminByAccountAndAdminPwd(adminAccount, adminPwd);
    }

    @Override
    public Page<Admin> selectAdminInfo(Admin admin, Integer pageNum, Integer pageSize) {
        Specification<Admin> specification = new Specification<Admin>() {
            @Override
            public Predicate toPredicate(Root<Admin> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (admin.getLevel() != null && admin.getLevel() != -1) {
                    list.add(cb.equal(root.get("level"), admin.getLevel()));
                }
                if (admin.getArea() != null && admin.getArea() !="") {
                    list.add(cb.like(root.get("area"),"%"+admin.getArea()+"%"));
                }

                    list.add(cb.equal(root.get("isDel"), 0));

                if(admin.getAdminId()!=null){
                    list.add(cb.equal(root.get("adminId"),admin.getAdminId()));
                }
                if(admin.getAccount()!=null && admin.getAccount()!=""){
                    list.add(cb.equal(root.get("account"),admin.getAccount()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        };
        Pageable pageable = new PageRequest(pageNum = pageNum == null ? 0 : pageNum, pageSize = pageSize == null ? 1 : pageSize, new Sort(new Sort.Order(Sort.Direction.DESC, "enterTime")));
        return adminReposity.findAll(specification,pageable);

    }

    @Override
    public Integer addAdmin(Admin admin) {
        Admin admin1=adminReposity.save(admin);
        if(admin1!=null){
            return 1;
        }
        return 0;
    }

    @Override
    public Admin findInfoByFatherId(Integer adminId) {
        return adminReposity.findInfoByFatherId(adminId);
    }

    @Override
    public Integer delAdmin(Integer adminId) {
        Admin admin=adminReposity.findOne(adminId);
       admin.setIsDel(1);
        Admin admin1=adminReposity.save(admin);
       if(admin1!=null){
           return 1;
       }
        return 0;
    }

    @Override
    public Integer isDisable(Integer adminId) {
        Admin admin=adminReposity.findOne(adminId);
        Integer isDisable=admin.getIsDisable();
        admin.setIsDisable(1);
        Admin admin1=adminReposity.save(admin);
        if(admin1!=null){
            return 1;
        }
        return 0;
    }
    @Override
    public Integer isDisable1(Integer adminId) {
        Admin admin=adminReposity.findOne(adminId);
        Integer isDisable=admin.getIsDisable();
        admin.setIsDisable(0);
        Admin admin1=adminReposity.save(admin);
        if(admin1!=null){
            return 1;
        }
        return 0;
    }

    @Override
    public Integer updateinfo(Admin admin) {
        Admin admin1=adminReposity.findOne(admin.getAdminId());
        if(admin.getAccount()!=null && admin.getAccount()!=""){
            admin1.setAccount(admin.getAccount());
        }
        if(admin.getAdminPwd()!=null && admin.getAdminPwd()!=""){
            admin1.setAdminPwd(admin.getAdminPwd());
        }
        if(admin.getFatherId()!=null){
            admin1.setFatherId(admin.getFatherId());
        }
        if(admin.getArea()!=null && admin.getArea()!=""){
            admin1.setArea(admin.getArea());
        }
        if(admin.getAlipay()!=null && admin.getAlipay()!=""){
            admin1.setAlipay(admin.getAlipay());
        }if(admin.getWeChat()!=null && admin.getWeChat()!=""){
            admin1.setWeChat(admin.getWeChat());
        }if(admin.getEnterTime()!=null){
            admin1.setEnterTime(admin.getEnterTime());
        }if(admin.getLevel()!=null){
            admin1.setLevel(admin.getLevel());
        }if(admin.getAdminTel()!=null){
            admin1.setAdminTel(admin.getAdminTel());
        }
        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        admin1.setLastLoginTime(nousedate);
        Admin admin2=adminReposity.save(admin1);
        if(admin2!=null){
            return 1;
        }
        return 0;
    }

    /**
     * 查询所有的代理商(张泽强)
     * @return
     * @throws SQLException
     */
    @Override
    public List<Admin> getAllAdminInfo() throws SQLException {
        return adminReposity.findAll();
    }
}
